﻿Public Class BuscPedido

    Dim classController As New Class_RC_Controller.rc_Notas()
    Dim clientes As New Class_RC_Controller.rc_Clientes

    Private _Cliente As String = ""

    Public Property pCliente() As String
        Get
            Return _Cliente
        End Get
        Set(value As String)
            _Cliente = value
        End Set
    End Property

    Private _pRuc As String = ""

    Public Property pRuc() As String
        Get
            Return _pRuc
        End Get
        Set(value As String)
            _pRuc = value
        End Set
    End Property

    Private _pContacto As String = ""

    Public Property pContacto() As String
        Get
            Return _pContacto
        End Get
        Set(value As String)
            _pContacto = value
        End Set
    End Property

    Private _Estado As Integer = 0

    Public Property pEstado() As Integer
        Get
            Return _Estado
        End Get
        Set(value As Integer)
            _Estado = value
            cboEstado.SelectedValue = value
        End Set
    End Property

    Private _codNota As Integer = 0

    Public Property pCodNota() As Integer
        Get
            Return _codNota
        End Get
        Set(value As Integer)
            _codNota = value
        End Set
    End Property

    Private _nroNota As String = ""

    Public Property pNroNota() As String
        Get
            Return _nroNota
        End Get
        Set(value As String)
            _nroNota = value
            txtNro.Text = value
        End Set
    End Property

    Private _pSoloPendientes As Boolean = False

    Public Property soloPendientes() As Boolean
        Get
            Return _pSoloPendientes
        End Get
        Set(value As Boolean)
            _pSoloPendientes = value
        End Set
    End Property

    Private _fechaNota As DateTime = Date.Now

    Public Property fechaNota() As DateTime
        Get
            Return _fechaNota
        End Get
        Set(value As DateTime)
            _fechaNota = value
        End Set
    End Property

    Private _totalNota As Decimal = 0

    Public Property totalNota() As Decimal
        Get
            Return _totalNota
        End Get
        Set(value As Decimal)
            _totalNota = value
        End Set
    End Property


    Private Sub BuscPedido_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        dgPedidos.AutoGenerateColumns = False
        fechaInicio.Value = DateTime.Now.AddDays(-5)

        CargarCombos()
        CargarNotas()
    End Sub

    Private Sub CargarNotas()

        Dim codEstado As Integer = 0
        If cbxEstado.Checked Then
            codEstado = cboEstado.SelectedValue
        End If

        Dim dt As New DataTable
        dt = classController.get_Notas_All(oiml.dbConn, 0, fechaInicio.Value, fechaFin.Value, 0, txtNombre.Text,
        codEstado, txtNro.Text, txtRuc.Text, txtContacto.Text, soloPendientes, oiml.codEstadoCierreNota)
        dgPedidos.DataSource = dt

        If Not dt Is Nothing Then

            If dt.Rows.Count > 0 Then
                dgPedidos.Select()
            End If

        Else

            If Not classController.MessageError Is Nothing Then
                MessageBox.Show("Error: " & classController.MessageError.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            ElseIf txtNro.Text <> "" Then
                MessageBox.Show("No se registra algun pedido pendiente con ese numero.", "Pedidos", MessageBoxButtons.OK, MessageBoxIcon.Information)
            End If

        End If

    End Sub

    Private Sub CargarCombos()
        Dim ds As DataTable

        If soloPendientes Then
            ds = classController.get_estado_notas(oiml.dbConn, oiml.codEstadoCierreNota)
        Else
            ds = classController.get_estado_notas(oiml.dbConn, 0)
        End If

        With cboEstado
            .DataSource = ds
            .DisplayMember = "descEstadoNota"
            .ValueMember = "codEstadoNota"
        End With

        cboEstado.SelectedIndex = 0

    End Sub

    Private Sub txtBuscar_Click(sender As System.Object, e As System.EventArgs) Handles txtBuscar.Click
        CargarNotas()
    End Sub

    Private Sub txtNombre_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles txtNombre.KeyDown
        If e.KeyCode = Keys.Enter Then
            CargarNotas()

        End If
    End Sub

    Private Sub dgPedidos_DoubleClick(sender As System.Object, e As System.EventArgs) Handles dgPedidos.DoubleClick
        Seleccionar()
    End Sub

    Private Sub dgPedidos_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles dgPedidos.KeyDown
        If e.KeyCode = Keys.Enter Then
            Seleccionar()
        End If

    End Sub



    Private Sub txtRuc_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles txtRuc.KeyDown
        If e.KeyCode = Keys.Enter Then
            CargarNotas()

        End If
    End Sub

    Private Sub txtNro_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles txtNro.KeyDown
        If e.KeyCode = Keys.Enter Then
            CargarNotas()

        End If
    End Sub

    Private Sub cbxEstado_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles cbxEstado.KeyDown
        If e.KeyCode = Keys.Enter Then
            CargarNotas()

        End If
    End Sub

    Private Sub cboEstado_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles cboEstado.KeyDown
        If e.KeyCode = Keys.Enter Then
            CargarNotas()

        End If
    End Sub

    Private Sub txtContacto_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles txtContacto.KeyDown
        If e.KeyCode = Keys.Enter Then
            CargarNotas()
        End If

    End Sub

    Private Sub Seleccionar()
        Try
            If dgPedidos.SelectedRows.Count > 0 Then

                pCodNota = dgPedidos.SelectedRows(0).Cells(0).Value.ToString()
                pEstado = dgPedidos.SelectedRows(0).Cells(1).Value.ToString()
                fechaNota = dgPedidos.SelectedRows(0).Cells(2).Value.ToString()
                pNroNota = dgPedidos.SelectedRows(0).Cells(3).Value.ToString()
                pCliente = dgPedidos.SelectedRows(0).Cells(4).Value.ToString()
                pRuc = dgPedidos.SelectedRows(0).Cells(5).Value.ToString()
                pContacto = dgPedidos.SelectedRows(0).Cells(6).Value.ToString()

                totalNota = dgPedidos.SelectedRows(0).Cells(8).Value.ToString()

                Me.Close()

            End If

        Catch ex As Exception
            MessageBox.Show("Error: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)

        End Try
    End Sub

    Private Sub dgPedidos_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgPedidos.CellContentClick

    End Sub
End Class